package com.zy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.entity.Email;
import com.zy.entity.Schedule;
import com.zy.entity.User;
import com.zy.entity.UserSchedule;
import com.zy.service.impl.EmailServiceImpl;
import com.zy.service.impl.ScheduleServiceImpl;
import com.zy.service.impl.UserScheduleServiceImpl;
import com.zy.service.impl.UserServiceImpl;
import com.zy.tools.appletsTools.TimeTool;
import com.zy.tools.appletsTools.TrimTool;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/app")
public class ScheduleController {
    /**
     * 小程序-日程
     *
     * @author 小熊
     * @date 2021-04-06 09:27
     */

    @Autowired
    ScheduleServiceImpl scheduleService;

    @Autowired
    EmailServiceImpl emailService;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    UserScheduleServiceImpl userScheduleService;

    @Autowired
    TimeTool timeTool;

    @Autowired
    TrimTool trimTool;

    /**
     * 日程初始化
     *
     * @param id,date
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSchedule", produces = "text/plain;charset=utf-8")
    public String getSchedule(String id, String date) {
        Integer Id = Integer.parseInt(id);//小程序前台传过来的用户id
        List<Schedule> schedules = scheduleService.getScheduleByUserId(Id, date);
        String result = JSON.toJSONString(schedules);
        return result;
    }

    /**
     * 添加日程
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addSchedule")
    public String addSchedule(HttpServletRequest request) throws JSONException {
        String title = request.getParameter("title");
        String startTime = timeTool.stampToDate(request.getParameter("startTime"));
        String endTime = timeTool.stampToDate(request.getParameter("endTime"));
        String createTime = request.getParameter("createTime");
        String[] user = trimTool.trimId(request.getParameter("users"));
        List<String> users = new ArrayList<>();
        for (int i = 0; i < user.length; i++) {
            users.add(user[i]);
        }
        String content = request.getParameter("content");
        String address = request.getParameter("address");
        String remind = request.getParameter("remind");
        Integer promoter = Integer.parseInt(request.getParameter("promoter"));
        Schedule schedule = new Schedule(title, startTime, endTime, address, users, content, remind, promoter, createTime);
        Integer scheduleId = scheduleService.addSchedule(schedule);
        if (scheduleId != -1) {
            if (users.size() != 1) {
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String now = simpleDateFormat.format(date);//获取当前的日期并转为字符串
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("id", promoter);
                User u = userService.getOne(queryWrapper);//根据用户id获取用户
                String name = u.getName();//获取用户的姓名
                String post = u.getPost();//获取用户的职位
                for (int i = 0; i < user.length - 1; i++) {//循环为每位加入日程或会议的用户发送邮件并将在user_schedule表插入数据
                    String emailContent = name + "邀请您参与(" + title + "),请您收到信息后尽快确认！";
                    emailService.save(new Email(Integer.parseInt(user[i]), emailContent, now, Integer.toString(schedule.getId())));
                    userScheduleService.save(new UserSchedule(Integer.parseInt(user[i]), schedule.getId()));
                }
            }
            return getSchedule(request.getParameter("promoter"), createTime);
        } else {
            return null;
        }
    }
}
